package com.sanqing.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.sanqing.base.action.BaseAction;
import com.sanqing.po.Course;
import com.sanqing.po.Examine;
import com.sanqing.po.Questions;
import com.sanqing.po.Subject;
import com.sanqing.service.CourseService;
import com.sanqing.service.ExamineService;
import com.sanqing.util.Page;
import com.sanqing.util.PageResult;

@Results({ 
	 @Result(name = "list", location = "/teacher/examine/examine-list.jsp"), 
	 @Result(name = "input", location = "/teacher/examine/examine-add.jsp"), 
	 @Result(name = "edit", location = "/teacher/examine/examine-edit.jsp"),	 
	 @Result(name = "customized", location = "/teacher/examine/examine-customized.jsp"), 
	 @Result(name = "view", location = "/teacher/examine/examine-view.jsp"),
	 @Result(name = "exp", location = "/teacher/examine/examine-exp.jsp"),
	 @Result(name = "success", location = "examine.action", type = "redirect")

})
public class ExamineAction extends BaseAction<Examine> {

	private static final long serialVersionUID = 7795872940645683836L;
	private Examine examine = new Examine();
	private int currentPage;
	@Autowired
	private ExamineService examineService;
	
	@Autowired
	private CourseService courseService;
	/**
	 * 查询考试信息
	 */
	@Override
	public String list() throws Exception {
		Page page = new Page();
		page.setEveryPage(10);// 每页显示10条记录
		page.setCurrentPage(currentPage);// 设置当前页
		PageResult pageResult = examineService.queryExamineByPage(page, examine);
		getRequest().setAttribute("pageResult", pageResult);
		getRequest().setAttribute("examineName", examine.getExamineName());
		return "list";
	}

	public String input() {
		List<Course> courses = courseService.getAll();
		getRequest().setAttribute("courses", courses);		
		return "input";
	}

	/**
	 * 信息考试信息
	 * 
	 * @return
	 */
	public String add() {
		try {
			examineService.add(examine);
			this.addActionMessage("新增考试成功!");
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage("新增考试失败：" + e.getMessage());
		}
		return SUCCESS;
	}
	
	public String edit() {
		List<Course> courses = courseService.getAll();
		getRequest().setAttribute("courses", courses);
		examine = examineService.getById(examine.getId());
		return "edit";
	}
	/**
	 * 更新考试信息
	 * @return
	 */
	public String update() {
		try {
			examineService.edit(examine);
			this.addActionMessage("修改考试成功!");
		} catch (Exception e) {
			e.printStackTrace();
			this.addActionMessage("修改考试失败：" + e.getMessage());
		}
		return SUCCESS;
	}
    /**
     * 删除信息
     * @return
     */
	public String delete() {
		try {
			examineService.delete(examine.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
    //定制试卷
	public String customized(){		
		examine = examineService.getById(examine.getId());
		
		List<Subject> chooseSubject = examineService.getSubject("1",examine.getCourse());
		List<Subject> decideList = examineService.getSubject("2", examine.getCourse());
		List<Subject> doubleDecideList = examineService.getSubject("3", examine.getCourse());
		
		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		return "customized";
	}
	
	public String customPaper(){
		examine = examineService.getById(examine.getId());
		String[] subjectID = getRequest().getParameterValues("subjectID");
		examineService.customPaper(examine,subjectID);
		return SUCCESS;
	}

	// 定制试卷
	public String view() {
		examine = examineService.getById(examine.getId());

		List<Questions> chooseSubject = examineService.getExamineSubject("1", examine.getCourse());
		List<Questions> decideList = examineService.getExamineSubject("2", examine.getCourse());
		List<Questions> doubleDecideList = examineService.getExamineSubject("3", examine.getCourse());

		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		return "view";
	}
	
	// 导出试卷
	public String exp() {
		examine = examineService.getById(examine.getId());

		List<Questions> chooseSubject = examineService.getExamineSubject("1", examine.getCourse());
		List<Questions> decideList =    examineService.getExamineSubject("2", examine.getCourse());
		List<Questions> doubleDecideList = examineService.getExamineSubject("3", examine.getCourse());

		getRequest().setAttribute("chooseSubject", chooseSubject);
		getRequest().setAttribute("decideList", decideList);
		getRequest().setAttribute("doubleDecideList", doubleDecideList);
		return "exp";
	}
	
	@Override
	public Examine getModel() {
		return examine;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public Examine getExamine() {
		return examine;
	}

	public void setExamine(Examine examine) {
		this.examine = examine;
	}

}
